package com.wangyb.wework.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.wangyb.wework.bo.WwTagAddBo;
import com.wangyb.wework.bo.WwTagEditBo;
import com.wangyb.wework.bo.WwTagQueryBo;
import com.wangyb.wework.entity.contact.WwTagGroup;
import com.wangyb.wework.service.contact.IWwTagService;
import com.wangyb.wework.vo.WwTagVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 客户标签Controller
 *
 * @author ruoyi
 * @date 2021-07-06
 */
@Api(value = "客户标签控制器", tags = {"客户标签管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wework/tag")
public class WwTagController extends BaseController {

    private final IWwTagService iWwTagService;

    /**
     * 查询客户标签列表
     */
    @ApiOperation("查询客户标签列表")
    @PreAuthorize("@ss.hasPermi('wework:tag:list')")
    @GetMapping("/list")
    public AjaxResult<List<WwTagGroup>> list(@Validated WwTagQueryBo bo) {
    	Long corpId = 1L;
		List<WwTagGroup> groupList = iWwTagService.getTagWithUsedNum(corpId);
		return AjaxResult.success(groupList);
    }

    /**
     * 导出客户标签列表
     */
    @ApiOperation("导出客户标签列表")
    @PreAuthorize("@ss.hasPermi('wework:tag:export')")
    @Log(title = "客户标签", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<WwTagVo> export(@Validated WwTagQueryBo bo) {
        List<WwTagVo> list = iWwTagService.queryList(bo);
        ExcelUtil<WwTagVo> util = new ExcelUtil<WwTagVo>(WwTagVo.class);
        return util.exportExcel(list, "客户标签");
    }

    /**
     * 获取客户标签详细信息
     */
    @ApiOperation("获取客户标签详细信息")
    @PreAuthorize("@ss.hasPermi('wework:tag:query')")
    @GetMapping("/{id}")
    public AjaxResult<WwTagVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iWwTagService.queryById(id));
    }

    /**
     * 新增客户标签
     */
    @ApiOperation("新增客户标签")
    @PreAuthorize("@ss.hasPermi('wework:tag:add')")
    @Log(title = "客户标签", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody WwTagAddBo bo) {
        return toAjax(iWwTagService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改客户标签
     */
    @ApiOperation("修改客户标签")
    @PreAuthorize("@ss.hasPermi('wework:tag:edit')")
    @Log(title = "客户标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody WwTagEditBo bo) {
        return toAjax(iWwTagService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除客户标签
     */
    @ApiOperation("删除客户标签")
    @PreAuthorize("@ss.hasPermi('wework:tag:remove')")
    @Log(title = "客户标签" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iWwTagService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
